package com.sysu.homework.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.homework.model.Homework;
import com.sysu.homework.model.HomeworkResult;
import com.sysu.homework.service.HomeworkResultService;
import com.sysu.homework.service.HomeworkService;
import com.sysu.homework.vo.HomeworkInfo;

@Component("homeworkAction")
@Scope("prototype")
public class HomeworkAction extends ActionSupport{
	private String title;
	private String content;
	private Date deadline;
	private Date createDate;
	private Date currentDate;
	
	private HomeworkService homeworkService;
	private List<Homework> homeworks;
	private List<HomeworkInfo> homeworkInfos = new ArrayList<HomeworkInfo>();
	private Homework homework;
	private HomeworkResultService homeworkResultService;
	

	HttpSession session = null;
	String role;
	int userid;

	private int id;
	
	public HomeworkAction() {
		session = ServletActionContext.getRequest().getSession();
		role = session.getAttribute("role").toString();
		userid = (Integer)session.getAttribute("user");
		
	}

	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=GBK");
		PrintWriter out = response.getWriter();
		
		createDate = new Date();
		Homework homework = new Homework();
		homework.setTitle(title);
		homework.setContent(content);
		homework.setDeadline(deadline);
		homework.setCreateDate(createDate);
		if(id>0) 
			homework.setId(id);
		if (homeworkService.addHomework(homework)>0){
			out.println(1);
			return null;
		}
		out.println(2);
		return null;
	}
	
	public String showHomework() {
		homeworks = homeworkService.showHomeworks();
		currentDate = new Date();
		role = session.getAttribute("role").toString();
			for (int i=0;i<homeworks.size();i++){
				HomeworkInfo info = new HomeworkInfo();
				info.setHomework(homeworks.get(i));
				info.setSubmit(false);
				if (homeworkResultService.isExistsResult(homeworks.get(i).getId(), userid)){
					info.setSubmit(true);
				}
				homeworkInfos.add(i,info);
			}
			
		homeworkInfos = getHomeworkInfos();
	
		return "show";
	}
	
	public String updateHomework() {
		homework = homeworkService.getHomeworkById(id);
		if (homework!=null) {
			return "update";
		}
		return "fail";
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getDeadline() {
		return deadline;
	}

	public void setDeadline(Date deadline) {
		this.deadline = deadline;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Resource
	public void setHomeworkService(HomeworkService homeworkService) {
		this.homeworkService = homeworkService;
	}

	public HomeworkService getHomeworkService() {
		return homeworkService;
	}

	public List<Homework> getHomeworks() {
		return homeworks;
	}

	public void setHomeworks(List<Homework> homeworks) {
		this.homeworks = homeworks;
	}

	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	public Date getCurrentDate() {
		return currentDate;
	}
	

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public void setHomework(Homework homework) {
		this.homework = homework;
	}

	public Homework getHomework() {
		return homework;
	}


	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setHomeworkInfos(List<HomeworkInfo> homeworkInfos) {
		this.homeworkInfos = homeworkInfos;
	}

	public List<HomeworkInfo> getHomeworkInfos() {
		return homeworkInfos;
	}
	

	public HomeworkResultService getHomeworkResultService() {
		return homeworkResultService;
	}

	@Resource
	public void setHomeworkResultService(HomeworkResultService homeworkResultService) {
		this.homeworkResultService = homeworkResultService;
	}
	
	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}
}
